﻿var Interface = function (name, methods) {
	if (arguments.length !== 2) {
		throw new Error("构造函数需要2个参数，您只提供了" + arguments.length + "个");
	}
	this.name = name;
	this.methods = [];
	for (var i = 0; i < methods.length; i++) {
		if (typeof methods[i] !== "string") {
			throw new Error("传入的方法名称必须为String类型");
		}
		this.methods.push(methods[i]);
	}
};
//接口的静态方法，用于检查实现接口的类是否实现了接口中的方法
Interface.ensureImplements = function (object) {
	if (arguments.length < 2) {
		throw new Error("至少需要2个参数，您只提供了" + arguments.length + "个");
	}
	for (var i = 1; i < arguments.length; i++) { //第二个参数开始
		var _interface = arguments[i];
		if (_interface.constructor !== Interface) {
			throw new Error("第" + i + "个参数必须为Interface的实例");
		}
	}
	for (var j = 0; j < _interface.methods.length; j++) { //检查实现
		var method = _interface.methods[j];
		if (!object[method] || typeof object[method] !== "function") {
			throw new Error("您没有实现" + _interface.name + "接口中的" + method + "方法");
		}
	}
}
 